package com.mmd.utils;

import com.mmd.bean.EnvType;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ApplicationConfigs {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfigs.class);
    private static Properties prop = new Properties();
    public static final EnvType envType;

    public ApplicationConfigs() {
    }

    public static String getConfig(String key) {
        return prop.getProperty(key);
    }

    public static EnvType getEnv() {
        String env = System.getProperty("spring.profiles.active");
        if (StringUtils.isBlank(env)) {
            env = "local";
        }

        EnvType type = EnvType.getEnv(env);
        if (type == null) {
            throw new IllegalArgumentException("环境配置错误");
        } else {
            return type;
        }
    }

    static {
        envType = EnvType.dev;
        String env = System.getProperty("spring.profiles.active");
        if (env == null || env.trim().isEmpty()) {
            env = "local";
        }

        String utilName = "/util-" + env.trim() + ".properties";
        LOGGER.info("load application config file:{}", utilName);
        InputStream uin = ApplicationConfigs.class.getResourceAsStream(utilName);
        if (uin != null) {
            try {
                prop.load(uin);
            } catch (IOException var13) {
                LOGGER.error("prop load exception", var13);
            }
        } else {
            LOGGER.error("cannot find properties:{}", uin);
        }

        String fileName2 = "/cf-" + env.trim() + ".properties";
        LOGGER.info("load application config file:{}", fileName2);
        InputStream in2 = ApplicationConfigs.class.getResourceAsStream(fileName2);
        if (in2 != null) {
            try {
                prop.load(in2);
            } catch (IOException var12) {
                LOGGER.error("prop load exception", var12);
            }
        } else {
            LOGGER.error("cannot find properties:{}", fileName2);
        }

        String cName = "/application.properties";
        LOGGER.info("load application config file:{}", cName);
        InputStream cin = ApplicationConfigs.class.getResourceAsStream(cName);
        if (cin != null) {
            try {
                prop.load(cin);
            } catch (IOException var11) {
                LOGGER.error("prop load exception", var11);
            }
        } else {
            LOGGER.error("cannot find properties:{}", cin);
        }

        String fileName = "/application-" + env.trim() + ".properties";
        LOGGER.info("load application config file:{}", fileName);
        InputStream in = ApplicationConfigs.class.getResourceAsStream(fileName);
        if (in != null) {
            try {
                prop.load(in);
            } catch (IOException var10) {
                LOGGER.error("prop load exception", var10);
            }
        } else {
            LOGGER.error("cannot find properties:{}", fileName);
        }

    }
}
